package com.example.gokuplayalong.service.fragments.users.profile;

import static android.text.InputType.TYPE_CLASS_NUMBER;
import static android.text.InputType.TYPE_NUMBER_FLAG_DECIMAL;

import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.gokuplayalong.MainActivity;
import com.example.gokuplayalong.R;
import com.example.gokuplayalong.bases.baiduapi.BaiduApi;
import com.example.gokuplayalong.bases.beans.AppParams;
import com.example.gokuplayalong.bases.beans.Beans;
import com.example.gokuplayalong.bases.custom.CustomGlideEngine;
import com.example.gokuplayalong.bases.custom.CustomImageUtils;
import com.example.gokuplayalong.bases.custom.textwatcher.CustomLimitInputTextWatcher;
import com.example.gokuplayalong.bases.custom.textwatcher.RegExUtil;
import com.example.gokuplayalong.bases.exception.ExceptionUtils;
import com.example.gokuplayalong.bases.network.NetWorkInterface;
import com.example.gokuplayalong.bases.network.updown.Upload;
import com.example.gokuplayalong.bases.network.updown.UploadDownInterface;
import com.example.gokuplayalong.bases.permission.PermissionUtils;
import com.example.gokuplayalong.bases.permission.PermissionUtilsInterface;
import com.example.gokuplayalong.bases.tools.Logger;
import com.example.gokuplayalong.bases.tools.Tools;
import com.example.gokuplayalong.service.fragmentsmanage.SysManagerFragments;
import com.kongzue.dialogx.dialogs.InputDialog;
import com.kongzue.dialogx.dialogs.MessageDialog;
import com.kongzue.dialogx.dialogs.PopTip;
import com.kongzue.dialogx.interfaces.OnBindView;
import com.kongzue.dialogx.interfaces.OnInputDialogButtonClickListener;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.SelectMimeType;
import com.luck.picture.lib.engine.CompressFileEngine;
import com.luck.picture.lib.engine.CropFileEngine;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnResultCallbackListener;
import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.NetworkPolicy;
import com.squareup.picasso.Picasso;
import com.yalantis.ucrop.UCrop;

import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

import pub.devrel.easypermissions.AppSettingsDialog;
import top.zibin.luban.Luban;
import top.zibin.luban.OnNewCompressListener;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link UserMainProfileMerchantInfo#newInstance} factory method to
 * create an instance of this fragment.
 *
 */
public class UserMainProfileMerchantInfo extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    View rootView;
    private ImageView mUserMainReceiveUserinfoIv;
    private ImageView mUserMainReceiveUserinfoHeadshot;
    private TextView mUserMainReceiveUserinfoMerchantName;
    private TextView mUserMainReceiveUserinfoMerchantNameChange;
    private TextView mUserMainReceiveUserinfoMerchantManifesto;
    private TextView mUserMainReceiveUserinfoMerchantManifestoChange;
    private TextView mUserMainReceiveUserinfoMerchantScore;
    private TextView mUserMainReceiveUserinfoMerchantId;
    private TextView mUserMainReceiveUserinfoMerchantAddress;
    private TextView mUserMainReceiveUserinfoMerchantIntroduce;
    private TextView mUserMainReceiveUserinfoMerchantIntroduceEdit;
    private LinearLayout mUserMainReceiveUserinfoMerchantServices;
    private TextView mUserMainReceiveUserinfoMerchantServicesShow;
    private TextView mUserMainReceiveUserinfoMerchantServicesChange;
    private LinearLayout mUserMainReceiveUserinfoMerchantSkills;
    private TextView mUserMainReceiveUserinfoMerchantSkillsAdd;
    private LinearLayout mUserMainReceiveUserinfoMerchantGameProwess;
    private LinearLayout mUserMainReceiveUserinfoMerchantGameShow;
    private Button mUserMainReceiveUserinfoMerchantFind;

    Beans bean = new Beans();
    public static int uploadSuccessFlag = 0;

    JSONArray skillsList = new JSONArray();


    public UserMainProfileMerchantInfo() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment UserMainProfileMerchantInfo.
     */
    // TODO: Rename and change types and number of parameters
    public static UserMainProfileMerchantInfo newInstance(String param1, String param2) {
        UserMainProfileMerchantInfo fragment = new UserMainProfileMerchantInfo();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (rootView == null)
            rootView = inflater.inflate(R.layout.fragment_user_main_receive_user_info, container, false);
        initView();
        return rootView;
    }

    private void initView() {
        mUserMainReceiveUserinfoIv = rootView.findViewById(R.id.user_main_receive_userinfo_iv);
        mUserMainReceiveUserinfoHeadshot = rootView.findViewById(R.id.user_main_receive_userinfo_headshot);
        mUserMainReceiveUserinfoMerchantName = rootView.findViewById(R.id.user_main_receive_userinfo_merchant_name);
        mUserMainReceiveUserinfoMerchantNameChange = rootView.findViewById(R.id.user_main_receive_userinfo_merchant_name_change);
        mUserMainReceiveUserinfoMerchantManifesto = rootView.findViewById(R.id.user_main_receive_userinfo_merchant_manifesto);
        mUserMainReceiveUserinfoMerchantManifestoChange = rootView.findViewById(R.id.user_main_receive_userinfo_merchant_manifesto_change);
        mUserMainReceiveUserinfoMerchantScore = rootView.findViewById(R.id.user_main_receive_userinfo_merchant_score);
        mUserMainReceiveUserinfoMerchantId = rootView.findViewById(R.id.user_main_receive_userinfo_merchant_id);
        mUserMainReceiveUserinfoMerchantAddress = rootView.findViewById(R.id.user_main_receive_userinfo_merchant_address);
        mUserMainReceiveUserinfoMerchantIntroduce = rootView.findViewById(R.id.user_main_receive_userinfo_merchant_introduce);
        mUserMainReceiveUserinfoMerchantIntroduceEdit = rootView.findViewById(R.id.user_main_receive_userinfo_merchant_introduce_edit);
        mUserMainReceiveUserinfoMerchantServices = rootView.findViewById(R.id.user_main_receive_userinfo_merchant_services);
        mUserMainReceiveUserinfoMerchantServicesShow = rootView.findViewById(R.id.user_main_receive_userinfo_merchant_services_show);
        mUserMainReceiveUserinfoMerchantServicesChange = rootView.findViewById(R.id.user_main_receive_userinfo_merchant_services_change);
        mUserMainReceiveUserinfoMerchantSkills = rootView.findViewById(R.id.user_main_receive_userinfo_merchant_skills);
        mUserMainReceiveUserinfoMerchantSkillsAdd = rootView.findViewById(R.id.user_main_receive_userinfo_merchant_skills_add);
        mUserMainReceiveUserinfoMerchantGameProwess = rootView.findViewById(R.id.user_main_receive_userinfo_merchant_game_prowess);
        mUserMainReceiveUserinfoMerchantGameShow = rootView.findViewById(R.id.user_main_receive_userinfo_merchant_game_show);
        mUserMainReceiveUserinfoMerchantFind = rootView.findViewById(R.id.user_main_receive_userinfo_merchant_find);

        mUserMainReceiveUserinfoMerchantServicesChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    if (!Tools.Deshak()) return;
                    MessageDialog.show("提示", "请填写服务内容")
                            .setCancelable(false)
                            .setCustomView(new OnBindView<MessageDialog>(R.layout.fragment_user_main_receive_user_info_service_change) {
                                @Override
                                public void onBind(MessageDialog dialog, View v) {
                                    try {

                                        LinearLayout lines = v.findViewById(R.id.user_main_receive_user_info_service_change_lines);
                                        List<LinearLayout> linearLayouts = new ArrayList<>();
                                        List<EditText> editTextsLabel = new ArrayList<>();
                                        List<EditText> editTextsDesc = new ArrayList<>();
                                        List<TextView> textViews = new ArrayList<>();

                                        v.findViewById(R.id.user_main_receive_user_info_service_change_add_lines)
                                                .setOnClickListener(new View.OnClickListener() {
                                                    @Override
                                                    public void onClick(View view) {
                                                        try {
                                                            LinearLayout childView = (LinearLayout) LayoutInflater.from(requireContext())
                                                                    .inflate(R.layout.fragment_user_main_receive_user_info_add_skills_template, lines, false);
                                                            lines.addView(childView);
                                                            linearLayouts.add(childView);

                                                            EditText editText = childView.findViewById(R.id.user_main_receive_userinfo_add_skills_template_label);
                                                            editText.setId(("edittext_merchant_info_" + linearLayouts.size() + "_label").hashCode());
                                                            editText.addTextChangedListener(new CustomLimitInputTextWatcher(editText));
                                                            editText.setHint("服务类型");
                                                            editText.setText("游戏陪玩");
                                                            editTextsLabel.add(editText);

                                                            editText = childView.findViewById(R.id.user_main_receive_userinfo_add_skills_template_desc);
                                                            editText.setId(("edittext_merchant_info_" + linearLayouts.size() + "_desc").hashCode());
                                                            editText.setHint("价格(元)/小时");
                                                            editText.setText(String.valueOf(0));
                                                            editText.setInputType(TYPE_CLASS_NUMBER | TYPE_NUMBER_FLAG_DECIMAL);
                                                            editTextsDesc.add(editText);

                                                            TextView textView = childView.findViewById(R.id.user_main_receive_userinfo_add_skills_template_delete);
                                                            textView.setId(("textview_merchant_info_" + linearLayouts.size() + "_delete").hashCode());
                                                            textView.setOnClickListener(new View.OnClickListener() {
                                                                @Override
                                                                public void onClick(View view) {
                                                                    try {
                                                                        for (int i = 0; i < textViews.size(); i++) {
                                                                            if (view.getId() == textViews.get(i)
                                                                                    .getId()) {
                                                                                lines.removeView(linearLayouts.get(i));
                                                                                linearLayouts.remove(i);
                                                                                editTextsLabel.remove(i);
                                                                                editTextsDesc.remove(i);
                                                                                textViews.remove(i);
                                                                                break;
                                                                            }
                                                                        }
                                                                    } catch (Throwable e) {
                                                                        ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                                                                    }
                                                                }
                                                            });
                                                            textViews.add(textView);


                                                        } catch (Throwable e) {
                                                            ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                                                        }
                                                    }
                                                });
                                        v.findViewById(R.id.user_main_receive_user_info_service_change_cancel)
                                                .setOnClickListener(new View.OnClickListener() {
                                                    @Override
                                                    public void onClick(View view) {
                                                        try {
                                                            dialog.dismiss();
                                                        } catch (Throwable e) {
                                                            ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                                                        }
                                                    }
                                                });
                                        v.findViewById(R.id.user_main_receive_user_info_service_change_submit)
                                                .setOnClickListener(new View.OnClickListener() {
                                                    @Override
                                                    public void onClick(View view) {
                                                        try {

                                                            JSONObject data = new JSONObject();


                                                            for (int i = 0; i < linearLayouts.size(); i++) {

                                                                if (editTextsLabel.get(i)
                                                                        .getText()
                                                                        .toString()
                                                                        .isEmpty() || editTextsDesc.get(i)
                                                                        .getText()
                                                                        .toString()
                                                                        .isEmpty()) {
                                                                    PopTip.show("未填写完成");
                                                                    return;
                                                                }

                                                                data.put(editTextsLabel.get(i)
                                                                        .getText()
                                                                        .toString(), editTextsDesc.get(i)
                                                                        .getText()
                                                                        .toString());
                                                            }

                                                            Beans beansIn = new Beans();
                                                            beansIn.getJsonObject()
                                                                    .put("phone", AppParams.getInstance().appCurrentUser.getJsonObject()
                                                                            .getString("phone"));
                                                            beansIn.getJsonObject()
                                                                    .put("modifyType", "servicetype");
                                                            beansIn.getJsonObject()
                                                                    .put("servicetype", data.toJSONString());

                                                            AppParams.getNewNetWork()
                                                                    .setInterface(new NetWorkInterface() {
                                                                        @Override
                                                                        public int success(String msg) {
                                                                            try {
                                                                                JSONObject msgFromServer = JSON.parseObject(msg);
                                                                                if (Objects.equals(msgFromServer.getString("result"), "成功")) {
                                                                                    PopTip.show("修改成功");
                                                                                    dialog.dismiss();
                                                                                    getMerchantInfo();
                                                                                }
                                                                            } catch (Throwable e) {
                                                                                ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                                                                            }
                                                                            return 0;
                                                                        }

                                                                        @Override
                                                                        public int failed(String msg) {
                                                                            return 0;
                                                                        }
                                                                    })
                                                                    .execute(AppParams.getInstance().baseUrl + "/modifyMerchantInfo", beansIn.getJsonObject()
                                                                            .toJSONString());
                                                        } catch (Throwable e) {
                                                            ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                                                        }
                                                    }
                                                });


                                    } catch (Throwable e) {
                                        ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                                    }
                                }
                            });
                } catch (Throwable e) {
                    ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                }
            }
        });

        mUserMainReceiveUserinfoMerchantSkillsAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    if (!Tools.Deshak()) return;
                    SysManagerFragments.switchOnSysFragment("UserMainReceiveUserInfoAddSkills");
                } catch (Throwable e) {
                    ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                }
            }
        });
        mUserMainReceiveUserinfoHeadshot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!Tools.Deshak()) return;
                try {
                    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) {
                        PermissionUtils.getInstance()
                                .checkPermission(PermissionUtils.permission_storage_sdk33, "悟空陪玩正在申请存储权限，用于上传截图、证明等，是否继续？", PermissionUtils.requestCode_storage_sdk33, new PermissionUtilsInterface() {
                                    @Override
                                    public void hasPermissions() {
                                        page2GkUpload();
                                    }

                                    @Override
                                    public void onPermissionsGranted(int i, @NonNull List<String> list) {
                                        page2GkUpload();
                                    }

                                    @Override
                                    public void onPermissionsDenied(int i, @NonNull List<String> list) {
                                        PopTip.show("权限被拒绝");
                                        new AppSettingsDialog.Builder(MainActivity.getInstance()).setTitle("无存储权限")
                                                .setRationale("\"悟空陪玩\"需要存储权限才能正常运行，打开设置界面开启存储权限")
                                                .setPositiveButton("去设置")
                                                .setNegativeButton("退出")
                                                .build()
                                                .show();

                                    }
                                });

                    }
                    else {
                        PermissionUtils.getInstance()
                                .checkPermission(PermissionUtils.permission_storage, "悟空陪玩正在申请存储权限，用于上传截图、证明等，是否继续？", PermissionUtils.requestCode_storage, new PermissionUtilsInterface() {
                                    @Override
                                    public void hasPermissions() {
                                        page2GkUpload();
                                    }

                                    @Override
                                    public void onPermissionsGranted(int i, @NonNull List<String> list) {
                                        page2GkUpload();
                                    }

                                    @Override
                                    public void onPermissionsDenied(int i, @NonNull List<String> list) {
                                        PopTip.show("权限被拒绝");
                                        new AppSettingsDialog.Builder(MainActivity.getInstance()).setTitle("无存储权限")
                                                .setRationale("\"悟空陪玩\"需要存储权限才能正常运行，打开设置界面开启存储权限")
                                                .setPositiveButton("去设置")
                                                .setNegativeButton("退出")
                                                .build()
                                                .show();
                                    }
                                });

                    }
                } catch (Throwable e) {
                    ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                }


            }
        });


        mUserMainReceiveUserinfoIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    if (!Tools.Deshak()) return;
                    SysManagerFragments.switchBackOnSysFragment();
                } catch (Throwable e) {
                    ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                }
            }
        });
        mUserMainReceiveUserinfoMerchantManifestoChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    if (!Tools.Deshak()) return;

                    new InputDialog("提示", "输入新宣言", "确定", "取消", "").setCancelable(false)
                            .setOkButton(new OnInputDialogButtonClickListener<InputDialog>() {
                                @Override
                                public boolean onClick(InputDialog baseDialog, View v, String inputStr) {

                                    BaiduApi.textCensor(RegExUtil.regexString(inputStr), new Handler(Looper.myLooper()) {
                                        @Override
                                        public void handleMessage(@NonNull Message msg) {
                                            super.handleMessage(msg);
                                            switch (msg.what) {
                                                case 1: {
                                                    Beans beansIn = new Beans();
                                                    beansIn.getJsonObject()
                                                            .put("phone", AppParams.getInstance().appCurrentUser.getJsonObject()
                                                                    .getString("phone"));
                                                    beansIn.getJsonObject()
                                                            .put("modifyType", "merchantManifesto");
                                                    beansIn.getJsonObject()
                                                            .put("merchantManifesto", RegExUtil.regexString(inputStr));
                                                    AppParams.getNewNetWork()
                                                            .setInterface(new NetWorkInterface() {
                                                                @Override
                                                                public int success(String msg) {
                                                                    try {
                                                                        JSONObject msgFromServer = JSON.parseObject(msg);
                                                                        if (Objects.equals(msgFromServer.getString("result"), "成功")) {
                                                                            PopTip.show("修改成功");

                                                                            JSONObject data = JSON.parseObject(bean.getJsonObject()
                                                                                    .getString("data"));
                                                                            data.put("merchantManifesto", RegExUtil.regexString(inputStr));
                                                                            bean.getJsonObject()
                                                                                    .put("data", data.toJSONString());
                                                                            UIRefresh();
                                                                        }
                                                                    } catch (Throwable e) {

                                                                    }
                                                                    return 0;
                                                                }

                                                                @Override
                                                                public int failed(String msg) {
                                                                    return 0;
                                                                }
                                                            })
                                                            .execute(AppParams.getInstance().baseUrl + "/modifyMerchantInfo", beansIn.getJsonObject()
                                                                    .toJSONString());

                                                }
                                                break;
                                                case 2: {
                                                    PopTip.show("内容不合规");
                                                }
                                                break;
                                            }
                                        }
                                    });


                                    return false;
                                }
                            })
                            .show();
                } catch (Throwable e) {
                    ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                }
            }
        });

        mUserMainReceiveUserinfoMerchantNameChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    if (!Tools.Deshak()) return;

                    new InputDialog("提示", "输入新名字", "确定", "取消", "").setCancelable(false)
                            .setOkButton(new OnInputDialogButtonClickListener<InputDialog>() {
                                @Override
                                public boolean onClick(InputDialog baseDialog, View v, String inputStr) {

                                    BaiduApi.textCensor(RegExUtil.regexString(inputStr), new Handler(Looper.myLooper()) {
                                        @Override
                                        public void handleMessage(@NonNull Message msg) {
                                            super.handleMessage(msg);
                                            switch (msg.what) {
                                                case 1: {
                                                    Beans beansIn = new Beans();
                                                    beansIn.getJsonObject()
                                                            .put("phone", AppParams.getInstance().appCurrentUser.getJsonObject()
                                                                    .getString("phone"));
                                                    beansIn.getJsonObject()
                                                            .put("modifyType", "merchantName");
                                                    beansIn.getJsonObject()
                                                            .put("merchantName", RegExUtil.regexString(inputStr));
                                                    AppParams.getNewNetWork()
                                                            .setInterface(new NetWorkInterface() {
                                                                @Override
                                                                public int success(String msg) {
                                                                    try {
                                                                        JSONObject msgFromServer = JSON.parseObject(msg);
                                                                        if (Objects.equals(msgFromServer.getString("result"), "成功")) {
                                                                            PopTip.show("修改成功");

                                                                            JSONObject data = JSON.parseObject(bean.getJsonObject()
                                                                                    .getString("data"));
                                                                            data.put("merchantName", RegExUtil.regexString(inputStr));
                                                                            bean.getJsonObject()
                                                                                    .put("data", data.toJSONString());
                                                                            UIRefresh();
                                                                        }
                                                                    } catch (Throwable e) {

                                                                    }
                                                                    return 0;
                                                                }

                                                                @Override
                                                                public int failed(String msg) {
                                                                    return 0;
                                                                }
                                                            })
                                                            .execute(AppParams.getInstance().baseUrl + "/modifyMerchantInfo", beansIn.getJsonObject()
                                                                    .toJSONString());

                                                }
                                                break;
                                                case 2: {
                                                    PopTip.show("名称不合规");
                                                }
                                                break;
                                            }
                                        }
                                    });


                                    return false;
                                }
                            })
                            .show();
                } catch (Throwable e) {
                    ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                }
            }
        });

        mUserMainReceiveUserinfoMerchantIntroduceEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    if (!Tools.Deshak()) return;

                    new InputDialog("提示", "输入新介绍", "确定", "取消", "").setCancelable(false)
                            .setOkButton(new OnInputDialogButtonClickListener<InputDialog>() {
                                @Override
                                public boolean onClick(InputDialog baseDialog, View v, String inputStr) {
                                    BaiduApi.textCensor(RegExUtil.regexString(inputStr), new Handler(Looper.myLooper()) {
                                        @Override
                                        public void handleMessage(@NonNull Message msg) {
                                            super.handleMessage(msg);
                                            switch (msg.what) {
                                                case 1: {
                                                    Beans beansIn = new Beans();
                                                    beansIn.getJsonObject()
                                                            .put("phone", AppParams.getInstance().appCurrentUser.getJsonObject()
                                                                    .getString("phone"));
                                                    beansIn.getJsonObject()
                                                            .put("modifyType", "merchantIntroduce");
                                                    beansIn.getJsonObject()
                                                            .put("merchantIntroduce", RegExUtil.regexString(inputStr));
                                                    AppParams.getNewNetWork()
                                                            .setInterface(new NetWorkInterface() {
                                                                @Override
                                                                public int success(String msg) {
                                                                    try {
                                                                        JSONObject msgFromServer = JSON.parseObject(msg);
                                                                        if (Objects.equals(msgFromServer.getString("result"), "成功")) {
                                                                            PopTip.show("修改成功");

                                                                            JSONObject data = JSON.parseObject(bean.getJsonObject()
                                                                                    .getString("data"));
                                                                            data.put("merchantIntroduce", RegExUtil.regexString(inputStr));
                                                                            bean.getJsonObject()
                                                                                    .put("data", data.toJSONString());
                                                                            UIRefresh();
                                                                        }
                                                                    } catch (Throwable e) {

                                                                    }
                                                                    return 0;
                                                                }

                                                                @Override
                                                                public int failed(String msg) {
                                                                    return 0;
                                                                }
                                                            })
                                                            .execute(AppParams.getInstance().baseUrl + "/modifyMerchantInfo", beansIn.getJsonObject()
                                                                    .toJSONString());

                                                }
                                                break;
                                                case 2: {
                                                    PopTip.show("内容不合规");
                                                }
                                                break;
                                            }
                                        }
                                    });


                                    return false;
                                }
                            })
                            .show();
                } catch (Throwable e) {
                    ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                }
            }
        });


    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            initChanged();
        }
    }

    private void initChanged() {
        try {
            String filename = AppParams.getInstance().appCurrentUser.getJsonObject()
                    .getString("phone") + "_merchant_headshot_upload.png";

            if (uploadSuccessFlag == 0) {
                Picasso.with(MainActivity.getInstance())
                        .load(AppParams.getInstance().uploadMediaLocationServer + "/" + filename)
//                        .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
                        .networkPolicy(NetworkPolicy.NO_CACHE)
                        .into(mUserMainReceiveUserinfoHeadshot);
            }
            else if (uploadSuccessFlag == -1) {
                uploadSuccessFlag = 0;
                Picasso.with(MainActivity.getInstance())
                        .load(AppParams.getInstance().uploadMediaLocationServer + "/" + filename)
                        .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
                        .networkPolicy(NetworkPolicy.NO_CACHE)
                        .into(mUserMainReceiveUserinfoHeadshot);
            }


            mUserMainReceiveUserinfoMerchantServicesChange.setVisibility(View.VISIBLE);
            mUserMainReceiveUserinfoMerchantNameChange.setVisibility(View.VISIBLE);
            mUserMainReceiveUserinfoMerchantManifestoChange.setVisibility(View.VISIBLE);
            mUserMainReceiveUserinfoMerchantIntroduceEdit.setVisibility(View.VISIBLE);
            mUserMainReceiveUserinfoMerchantSkillsAdd.setVisibility(View.VISIBLE);
            getMerchantInfo();
        } catch (Throwable e) {

            ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
        }
    }


    private void getMerchantInfo() {
        try {

            Beans beansIn = new Beans();
            beansIn.getJsonObject()
                    .put("phone", AppParams.getInstance().appCurrentUser.getJsonObject()
                            .getString("phone"));

            AppParams.getNewNetWork()
                    .setInterface(new NetWorkInterface() {
                        @Override
                        public int success(String msg) {

                            requireActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    try {
                                        JSONObject msgFromServer = JSON.parseObject(msg);
                                        if (Objects.equals(msgFromServer.getString("result"), "成功")) {

                                            bean.setJsonObject(JSON.parseObject(msgFromServer.getString("merchantInfo")));

                                            skillsList = JSON.parseArray(msgFromServer.getString("skillsList"));

                                            UIRefresh();
                                        }
                                    } catch (Throwable e) {
                                        ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                                    }
                                }
                            });
                            return 0;
                        }

                        @Override
                        public int failed(String msg) {
                            return 0;
                        }
                    })
                    .execute(AppParams.getInstance().baseUrl + "/getMerchantInfo", beansIn.getJsonObject()
                            .toJSONString());
        } catch (Throwable e) {
            ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
        }
    }

    void UIRefresh() {
        try {
            mUserMainReceiveUserinfoMerchantId.setText("ID:" + bean.getJsonObject()
                    .getString("merchantid"));

            JSONObject data = JSON.parseObject(bean.getJsonObject().getString("data"));
            if (data != null) {

                if (!StringUtils.isEmpty(data.getDoubleValue("merchantScore") + "")) {
                    mUserMainReceiveUserinfoMerchantScore.setText(String.format("%s分", data.getDoubleValue("merchantScore")));
                }
                else {
                    mUserMainReceiveUserinfoMerchantScore.setText("0.0分");
                }
                if (!StringUtils.isEmpty(data.getString("merchantName"))) {
                    mUserMainReceiveUserinfoMerchantName.setText(data.getString("merchantName"));
                }
                else {
                    if (Objects.equals(data.getString("bodyType"), "企业") || Objects.equals(data.getString("bodyType"), "个体工商户")) {
                        mUserMainReceiveUserinfoMerchantName.setText(data.getString("companiesMerchantAbbreviation"));
                    }
                    else if (Objects.equals(data.getString("bodyType"), "个人")) {
                        mUserMainReceiveUserinfoMerchantName.setText(data.getString("individualMerchantAbbreviation"));
                    }
                }
                if (!StringUtils.isEmpty(data.getString("merchantIntroduce"))) {
                    mUserMainReceiveUserinfoMerchantIntroduce.setText(data.getString("merchantIntroduce"));
                }
                if (!StringUtils.isEmpty(data.getString("merchantManifesto"))) {
                    mUserMainReceiveUserinfoMerchantManifesto.setText(data.getString("merchantManifesto"));
                }

                try {
                    JSONObject servicetype = JSON.parseObject(bean.getJsonObject()
                            .getString("servicetype"));
                    if (servicetype != null) {
                        StringBuilder serviceType = new StringBuilder();
                        Iterator<String> iterator = servicetype.keySet().iterator();
                        while (iterator.hasNext()) {
                            String key = (String) iterator.next();
                            serviceType.append(key)
                                    .append(":")
                                    .append(servicetype.getString(key))
                                    .append("元/小时\n");
                        }
                        mUserMainReceiveUserinfoMerchantServicesShow.setText(serviceType.toString());

                    }
                    else {
                        mUserMainReceiveUserinfoMerchantServicesShow.setText("");
                    }
                } catch (Throwable e) {
                    ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                }


                mUserMainReceiveUserinfoMerchantSkills.removeAllViews();
                mUserMainReceiveUserinfoMerchantGameProwess.removeAllViews();
                mUserMainReceiveUserinfoMerchantGameShow.removeAllViews();

                List<TextView> skillsGameType = new ArrayList<>();

                for (int i = 0; i < skillsList.size(); i++) {

                    TextView textView = new TextView(rootView.getContext());
                    textView.setId(("UserMainProfileMerchantInfo_textview_merchant_skills_" + i + "_rand").hashCode());
                    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                    lp.setMargins(20, 20, 20, 20);
                    textView.setLayoutParams(lp);

                    textView.setTextSize(25);
                    textView.setTextColor(Color.BLACK);
                    textView.setText(skillsList.getJSONObject(i).getString("gametype"));

                    textView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            try {


                                mUserMainReceiveUserinfoMerchantGameProwess.removeAllViews();
                                mUserMainReceiveUserinfoMerchantGameShow.removeAllViews();

                                for (int i = 0; i < skillsGameType.size(); i++) {
                                    skillsGameType.get(i).setTextColor(Color.BLACK);
                                }

                                for (int i = 0; i < skillsGameType.size(); i++) {
                                    if (skillsGameType.get(i).getId() == view.getId()) {

                                        //显示标签
                                        skillsGameType.get(i).setTextColor(Color.BLUE);
                                        Iterator<String> sIterator = JSON.parseObject(skillsList.getJSONObject(i)
                                                .getString("data")).keySet().iterator();

                                        while (sIterator.hasNext()) {
                                            LinearLayout childView = (LinearLayout) LayoutInflater.from(requireContext())
                                                    .inflate(R.layout.fragment_user_main_receive_user_info_template, mUserMainReceiveUserinfoMerchantGameProwess, false);

                                            String key = sIterator.next();
                                            if (!Objects.equals(key, "imageViewsName")) {
                                                TextView textView1 = childView.findViewById(R.id.user_main_receive_user_info_template_label);
                                                textView1.setText(key + ":");
                                                textView1 = childView.findViewById(R.id.user_main_receive_user_info_template_desc);
                                                textView1.setText(JSON.parseObject(skillsList.getJSONObject(i)
                                                        .getString("data")).getString(key));
                                                mUserMainReceiveUserinfoMerchantGameProwess.addView(childView);

                                            }


//                                            显示图片

                                            mUserMainReceiveUserinfoMerchantGameShow.removeAllViews();

                                            List<String> imageViewsName = new ArrayList<>(Arrays.asList(JSON.parseObject(skillsList.getJSONObject(i)
                                                            .getString("data"))
                                                    .getString("imageViewsName")
                                                    .split(",")));

                                            for (int j = 0; j < imageViewsName.size(); j++) {
                                                ImageView imageView = new ImageView(rootView.getContext());

                                                imageView.setId(("UserMainProfileMerchantInfo_imageview_merchant_skills_" + j + "_rand").hashCode());

                                                Picasso.with(MainActivity.getInstance())
                                                        .load(AppParams.getInstance().uploadMediaLocationServer + "/" + imageViewsName.get(j))
                                                        .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
                                                        .networkPolicy(NetworkPolicy.NO_CACHE)
                                                        .into(imageView);

                                                mUserMainReceiveUserinfoMerchantGameShow.addView(imageView);
                                            }
                                        }
                                    }
                                }


                            } catch (Throwable e) {
                                ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                            }
                        }
                    });
                    skillsGameType.add(textView);
                    mUserMainReceiveUserinfoMerchantSkills.addView(textView);
                }

                if (!skillsGameType.isEmpty()) skillsGameType.get(0).performClick();

            }
        } catch (Throwable e) {
            ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
        }
    }

    private void page2GkUpload() {
        try {

            PictureSelector.create(MainActivity.getInstance())
                    .openGallery(SelectMimeType.ofImage())

                    .setCompressEngine((CompressFileEngine) ((context, source, call) -> {
                        Luban.with(context)
                                .load(source)
                                .setCompressListener(new OnNewCompressListener() {
                                    @Override
                                    public void onStart() {
                                        Logger.d("onStart");
                                    }

                                    @Override
                                    public void onSuccess(String source, File compressFile) {
                                        Logger.d("onSuccess");
                                        if (call != null) {
                                            call.onCallback(source, compressFile.getAbsolutePath());
                                        }
                                    }

                                    @Override
                                    public void onError(String source, Throwable e) {
                                        Logger.d("onError " + e.getMessage());
                                        if (call != null) {
                                            call.onCallback(source, null);
                                        }
                                    }
                                })
                                .launch();
                    }))
                    .isDisplayCamera(false)
                    .setMaxSelectNum(1)
                    .setCropEngine(new CropFileEngine() {
                        @Override
                        public void onStartCrop(Fragment fragment, Uri srcUri, Uri destinationUri, ArrayList<String> dataSource, int requestCode) {
                            try {
                                UCrop.of(srcUri, destinationUri)
                                        .withAspectRatio(1, 1)
                                        .start(fragment.requireContext(), fragment, requestCode);
                            } catch (Throwable e) {
                                ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                            }
                        }
                    })
                    .setImageEngine(CustomGlideEngine.createGlideEngine())
                    .forResult(new OnResultCallbackListener<LocalMedia>() {
                        @Override
                        public void onResult(ArrayList<LocalMedia> result) {
                            if (!result.isEmpty()) {
                                LocalMedia localMedia = (LocalMedia) result.get(0);

                                BaiduApi.imageEncodedCensor(CustomImageUtils.encodeImageToBase64(new File(localMedia.getCompressPath())), new Handler(Looper.myLooper()) {
                                    @Override
                                    public void handleMessage(@NonNull Message msg) {
                                        super.handleMessage(msg);
                                        switch (msg.what) {
                                            case 1: {
                                                String filename = AppParams.getInstance().appCurrentUser.getJsonObject()
                                                        .getString("phone") + "_merchant_headshot_upload.png";

                                                Upload.getInstance()
                                                        .uploadImage(new File(localMedia.getCompressPath()), localMedia.getMimeType(), filename, new UploadDownInterface() {
                                                            @Override
                                                            public void uploadSuccess(String msg) {
                                                                requireActivity().runOnUiThread(new Runnable() {
                                                                    @Override
                                                                    public void run() {
                                                                        try {
                                                                            uploadSuccessFlag = 1;
                                                                            PopTip.show("上传成功!");
                                                                            Picasso.with(MainActivity.getInstance())
                                                                                    .load(AppParams.getInstance().uploadMediaLocationServer + "/" + filename)
                                                                                    .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
                                                                                    .networkPolicy(NetworkPolicy.NO_CACHE)
                                                                                    .into(mUserMainReceiveUserinfoHeadshot);
                                                                        } catch (Throwable e) {
                                                                            ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                                                                        }
                                                                    }
                                                                });
                                                            }

                                                            @Override
                                                            public void uploadFailed(IOException e) {
                                                                PopTip.show("上传失败!");
                                                            }

                                                            @Override
                                                            public void downSuccess() {

                                                            }

                                                            @Override
                                                            public void downFailed() {

                                                            }
                                                        });

                                            }
                                            break;
                                            case 2: {
                                                PopTip.show("图片不合规，请重新上传!");
                                            }
                                            break;
                                        }
                                    }
                                });

                            }
                        }

                        @Override
                        public void onCancel() {

                        }
                    });

        } catch (Throwable e) {
            ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
        }
    }
}